package com.example.orientatonviewpagertest.view;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.orientatonviewpagertest.R;

public class ViewPagerView extends LinearLayout {

	private ViewPager viewpager_orientation;
	
	TextView textview_text;

	MyPagerAdapter myPagerAdapter;
	
	private Context mContext;

	public ViewPagerView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView(context);
		initData();
		setListener();
	}

	public ViewPagerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
		initData();
		setListener();
	}

	public ViewPagerView(Context context) {
		super(context);
		initView(context);
		initData();
		setListener();
	}

	private void setListener() {

	}

	private void initData() {

	}
	
	public void setdata(int position){
		textview_text.setText("测试view" + position);
	}

	private void initView(Context context) {
		mContext = context;
		View view = LayoutInflater.from(context).inflate(
				R.layout.item_viewpager, this);
		viewpager_orientation = (ViewPager) view
				.findViewById(R.id.viewpager_orientation);
		textview_text = (TextView) view
				.findViewById(R.id.textview_text);
		myPagerAdapter = new MyPagerAdapter();
		viewpager_orientation.setAdapter(myPagerAdapter);
	}

	class MyPagerAdapter extends PagerAdapter {

		// private int curentPosition;

		@Override
		public int getCount() {
			return 6;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		// public int getCurentPosition() {
		// return curentPosition;
		// }

		/**
		 * 颜色数据是包括：red，green，blue，gray 0是gray，5是red
		 */
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			ImageView imageView = new ImageView(mContext);
			imageView.setLayoutParams(new LayoutParams(
					LayoutParams.MATCH_PARENT, 240));
			switch (position) {
			case 0:
				imageView.setBackgroundColor(Color.GRAY);
				break;
			case 1:
				imageView.setBackgroundColor(Color.RED);
				break;
			case 2:
				imageView.setBackgroundColor(Color.GREEN);
				break;
			case 3:
				imageView.setBackgroundColor(Color.BLUE);
				break;
			case 4:
				imageView.setBackgroundColor(Color.GRAY);
				break;
			case 5:
				imageView.setBackgroundColor(Color.RED);
				break;

			default:
				imageView.setBackgroundColor(Color.RED);
				break;
			}

			container.addView(imageView);
			return imageView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((ImageView) object);
		}

	}
}
